package com.itheima;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        test0();
        test1();
        test2();
        sum(1,2,3,4,5);
        System.out.println(sum(1,2,3,4,5));
    }

    private static void test0() {
        //集合只能存储引用数据类型
        TreeSet<Integer> set1 = new TreeSet<>();
//        //1-->包装成Integer对象-->自动装箱
//        Integer i1 = Integer.valueOf(1);
//        int a = i1 + 2;
//        //自动拆箱
//        System.out.println(a);
        set1.add(2);
        set1.add(4);
        set1.add(6);
        set1.add(8);
        set1.add(10);
        for (Integer integer : set1){
             System.out.println(integer);
        }
    }

    public  static  void  test1(){
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("四张",19));
        set.add(new Student("李旺",21));
        set.add(new Student("武七",16));
        set.add(new Student("王五",20));

        for (Student student: set ){
            System.out.println(student.getName() + "-----" + student.getAge());
            System.out.println("-------------------");
        }

    }

    public static void test2() {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //如果是正序排，那么o1 - o2
                //倒序则o2 - o1

                return o1.getAge() - o2.getAge();
            }
        };
        Comparator<Student> comparator1 = (o1, o2) -> o1.getAge() - o2.getAge();
            TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new

            Student("四张",23));
        set.add(new

            Student("李旺",21));
        set.add(new

            Student("五七",16));
        set.add(new

            Student("王五",18));
        for(
            Student student:set )

            {
                System.out.println(student.getName() + "-----" + student.getAge());
                System.out.println("-------------------");
            }
        }

    public static int sum(int... a){
        int total = 0;
        for (int i = 0; i < a.length; i++) {
            total += a[i];
        }
        return total;
    }

}
